<template>
	<view class="no-login">
		<image src="../../static/logo.png"></image>
		<button open-type="getUserInfo" @getuserinfo="getUserInfo">一键登录</button>
		<text>登陆后尽享更多权益</text>
	</view>
</template>

<script>
	import { mapMutations, mapState } from "vuex"
	export default {
		name: "no-login",
		data() {
			return {

			};
		},
		computed: {
			...mapState('m_user', ['redirectInfo'])
		},
		methods: {
			...mapMutations('m_user', ['setUserInfo']),
			getUserInfo(e) {
				var that = this
				console.log(e);
				this.setUserInfo(e.detail.userInfo)
				uni.login({
					provider: 'weixin',
					success: async function(loginRes) {
						console.log(loginRes.code);
						// 换取token
						const query = {
							code: "013pZWCt0GniKb1ftkBt0AxfDt0pZWCf",
							encryptedData: "FGMaNbO4ytTFTUzK3wmtfHWoFeQJYkyMajQ5tVbQglwmnJ46SMi43Thq0OZjSSa5Msmnpdfx7yigYkdvUbsiQFyF9xMGEQLpUFcpNjnYoCQOl2Ka5zEeqpMY91ywYAsPISeu1rwvFCU2Ucenxf7gdcDPQ/jNJdORGb86DR+2gVJeg8TZiXWJ66enpynj35DiaE1jNWUSzpgbOvO40dZrWnHVwDEJN+upeSHRR/IW7t3cgtV63/GeZzNhFbUYqNHO6M1KHetwY1u8mFXuZoXwVV7DrEwEzY1btqcfW93q4ekn2uwAUeJM4U+3roBsIT5ZpEM8YtQZVsAwjaWHBe8id1H4lurEFC5/GwLnRONrIiR7KDF7MPxnMO9flWFQEfay5dt9rBELzk1Efjf2H6UnzYfh+o4MbPxtc0cUiHEnNCVcXjDtulXSRUy7RZQPmjF/tYBHHrXEFubxnK4oRBZz/A==",
							iv: "y/ESFBaesbEzIbB1th5Knw==",
							rawData: "{\"nickName\":\"优购\",\"gender\":0,\"language\":\"zh_CN\",\"city\":\"\",\"province\":\"\",\"country\":\"\",\"avatarUrl\":\"https://wx.qlogo.cn/mmopen/vi_32/icWlxE4rARHaIlib1PRmBtRa2tQicUSEHYu8UIGZ0LLic9C0PticibED6brRFCuQYeLGtwTcBYFgMtcF11N31pVhMF8g/132\"}",
							signature: "d06cd3a54e89e6014e43694844706eaccad109bb"
						}
						const query1 = {
							code: loginRes.code,
							signature: e.detail.signature,
							rawData: e.detail.rawData,
							iv: e.detail.iv,
							encryptedData: e.detail.encryptedData
						}
						try{
							const res = await uni.$http.post('/api/public/v1/users/wxlogin', query1)
							console.log(res)
							that.navigateBack()
						}catch(e){
							//TODO handle the exception
							that.navigateBack()
						}
						
					}
				});
			},
			navigateBack() {
				console.log(2)
				if (this.redirectInfo && this.redirectInfo.openType === 'switchTab') {
					console.log(1)
					uni.switchTab({
						url: this.redirectInfo.from,
						complete: () => {
							this.updateRedirectInfo({})
						}
					})
				}
			}
		}
	}
</script>

<style lang="scss">
	.no-login {
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
		height: 360px;

		image {
			width: 100px;
			height: 100px;
			border-radius: 50%;
		}

		button {
			width: 90%;
			background-color: #c00000;
			border-radius: 40px;
			margin: 70rpx 0 35rpx;
			color: #fff;
		}

		text {
			color: #C0C0C0
		}
	}
</style>
